package com.linln.modules.member.service.impl;

import java.util.ArrayList;
import java.util.List;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Example;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.linln.common.data.PageSort;
import com.linln.modules.member.domain.MemberPayDayCount;
import com.linln.modules.member.repository.MemberPayDayCountRepository;
import com.linln.modules.member.service.MemberPayDayCountService;

/**
 * @author hct
 * @date 2019/09/12
 */
@Service
public class MemberPayDayCountServiceImpl implements MemberPayDayCountService {

    @Autowired
    private MemberPayDayCountRepository MemberPayDayCountRepository;

    /**
     * 根据ID查询数据
     * @param id 主键ID
     */
    @Override
    @Transactional
    public MemberPayDayCount getById(Long id) {
        return MemberPayDayCountRepository.findById(id).orElse(null);
    }
   
   @Override
    public   Boolean delCollect(Long id){
	   boolean isOk=false;
	   try{
		   MemberPayDayCountRepository.deleteById(id);
        isOk=true;
	   }catch(Exception e){
		   
	   }
       return isOk;
   }
    
    /**
     * 获取分页列表数据
     * @param example 查询实例
     * @return 返回分页数据
     */
    @Override
    public Page<MemberPayDayCount> getPageList(Example<MemberPayDayCount> example) {
        // 创建分页对象
        PageRequest page = PageSort.pageRequest();
        return MemberPayDayCountRepository.findAll(example, page);
    }
 
    /**
     * 获取分页列表数据
     * @param user 实体对象
     * @return 返回分页数据
     */
    @SuppressWarnings("serial")
	@Override
    @Transactional
    public Page<MemberPayDayCount> getPageList(String startTime,String endTime,String area) {
        // 创建分页对象
        PageRequest page = PageSort.pageRequest(Sort.Direction.ASC);

        // 使用Specification复杂查询
        return MemberPayDayCountRepository.findAll(new Specification<MemberPayDayCount>(){

            @Override
            public Predicate toPredicate(Root<MemberPayDayCount> root, CriteriaQuery<?> query, CriteriaBuilder cb) {
                List<Predicate> preList = new ArrayList<>();
                if(startTime!= null){
                    preList.add(cb.greaterThanOrEqualTo(root.get("createDate").as(String.class), startTime));
                     
                }
                
                if(endTime!= null){
                	  preList.add(cb.lessThanOrEqualTo(root.get("createDate").as(String.class), endTime));
                }                
                

                if(area!= null){
                	  preList.add(cb.equal(root.get("area").as(String.class), area));
                }                
                Predicate[] pres = new Predicate[preList.size()];
                return query.where(preList.toArray(pres)).getRestriction();
            }

        }, page);
    }

    
   public List<MemberPayDayCount> getList(Example<MemberPayDayCount> example){
        return MemberPayDayCountRepository.findAll(example);

    }

    /**
     * 保存数据
     * @param MemberPayDayCount 实体对象
     */
    @Override
    public MemberPayDayCount save(MemberPayDayCount memberPayDayCount) {
        return MemberPayDayCountRepository.save(memberPayDayCount);
    }

    public Integer insert() {
        if(MemberPayDayCountRepository.dayCount()>0){
            return 0;
        }
    	return MemberPayDayCountRepository.insert();
    }
}